﻿namespace Hidistro.UI.Web.Admin
{
    using Hidistro.ControlPanel.Store;
    using Hidistro.Membership.Context;
    using Hidistro.UI.ControlPanel.utility;
    using Hidistro.UI.Web;
    using System;
    using System.Text;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;

    [AdministerCheck(true)]
    public class BFDSet : AdminPage
    {
        protected HtmlGenericControl div_pan1;
        protected HtmlGenericControl div_pan2;
        protected HtmlGenericControl div_total;
        protected LinkButton hlinkCreate;
        protected LinkButton hplinkSet;
        protected LinkButton LkClose;
        protected HyperLink Lktoal;
        private string mykey = "123456";
        private string sign;

        protected void hlinkCreate_Click(object sender, EventArgs e)
        {
            SiteSettings siteSettings = HiContext.Current.SiteSettings;
            string host = this.Page.Request.Url.Host;
            string str2 = "123456";
            string str3 = "hishop";
            string str4 = Guid.NewGuid().ToString().Replace('-', '_');
            string str5 = host;
            StringBuilder builder = new StringBuilder();
            builder.Append("{");
            builder.AppendFormat("\"code\":\"{0}\",", str3);
            builder.AppendFormat("\"userName\":\"{0}\",", str4);
            builder.AppendFormat("\"password\":\"{0}\",", str5);
            builder.AppendFormat("\"realName\":\"{0}\",", "百达");
            builder.AppendFormat("\"linkName\":\"{0}\",", "长沙百达");
            builder.AppendFormat("\"phone\":\"{0}\",", "13088888888");
            builder.AppendFormat("\"email\":\"{0}\",", "admin@baida.com.cn");
            builder.AppendFormat("\"comName\":\"{0}\",", siteSettings.SiteName);
            builder.AppendFormat("\"comDomain\":\"{0}\",", host);
            builder.AppendFormat("\"comAddress\":\"{0}\",", "湖南长沙");
            builder.AppendFormat("\"bussiness\":\"{0}\",", "1,2");
            builder.AppendFormat("\"scope\":\"{0}\",", "6,7");
            builder.AppendFormat("\"pid\":\"{0}\"", "8");
            builder.Append("}");
            string str6 = APIHelper.Sign("data=" + builder.ToString() + "&mkey=" + str2, "md5", "utf-8");
            string postData = "data=" + builder.ToString() + "&sign=" + str6;
            string url = "http://passport.baifendian.com/bbgweb/callback/service.do";
            if (APIHelper.PostData(url, postData) == "1000")
            {
                siteSettings.EnabledBFD = false;
                siteSettings.BFDUserName = str4;
                this.div_pan1.Visible = false;
                this.div_pan2.Visible = true;
                this.div_total.Visible = false;
                this.hplinkSet.Text = "开启分析引擎";
                SettingsManager.Save(siteSettings);
                this.ShowMsg("创建账号成功", true);
            }
            else
            {
                this.ShowMsg("创建账号失败", false);
            }
        }

        protected void hplinkSet_Click(object sender, EventArgs e)
        {
            SiteSettings siteSettings = HiContext.Current.SiteSettings;
            this.div_pan1.Visible = false;
            this.div_pan2.Visible = false;
            this.div_total.Visible = true;
            siteSettings.EnabledBFD = true;
            SettingsManager.Save(siteSettings);
            this.ShowMsg("开启分析引擎功能成功", true);
        }

        protected void LkClose_Click(object sender, EventArgs e)
        {
            SiteSettings siteSettings = HiContext.Current.SiteSettings;
            if (siteSettings.EnabledBFD)
            {
                siteSettings.EnabledBFD = false;
                SettingsManager.Save(siteSettings);
                this.div_pan1.Visible = false;
                this.div_pan2.Visible = true;
                this.div_total.Visible = false;
                this.ShowMsg("关闭分析引擎功能成功", true);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.hlinkCreate.Click += new EventHandler(this.hlinkCreate_Click);
            this.hplinkSet.Click += new EventHandler(this.hplinkSet_Click);
            if (!base.IsPostBack)
            {
                SiteSettings siteSettings = HiContext.Current.SiteSettings;
                if (string.IsNullOrEmpty(siteSettings.BFDUserName))
                {
                    this.div_pan1.Visible = true;
                    this.div_pan2.Visible = false;
                    this.div_total.Visible = false;
                }
                else
                {
                    this.div_pan1.Visible = false;
                    this.div_pan2.Visible = true;
                    if (siteSettings.EnabledBFD)
                    {
                        this.div_pan2.Visible = false;
                        this.div_total.Visible = true;
                    }
                    else
                    {
                        this.div_total.Visible = false;
                    }
                }
            }
        }
    }
}

